package com.qunar.toku.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory;


@SuppressWarnings("unused")
public class ProxyStandard implements InvocationHandler {
	private final InternalLogger logger = 
			InternalLoggerFactory.getInstance(ProxyStandard.class);
	private Class<?> interfaceClass;
	private MethodCaller methodCaller;
	private String lookup;

	public ProxyStandard(Class<?> interfaceClass, String serviceName, String lookup) {
		this.interfaceClass = interfaceClass;
		this.lookup = lookup;
		this.methodCaller = new MethodCaller(serviceName);
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		Object clazz = Class.forName(lookup).newInstance();
		long start = System.currentTimeMillis();
		Future future = methodCaller.doMethodCall(clazz, method, args);
		long end = System.currentTimeMillis();
		long total = end - start;
		if (total > 200) {
            logger.warn("method:" + method.getName() + " invoke time :" + total);
        }
		return future.get(2000); //最多阻塞两秒返回

	}

}
